package com.zztl.infiniteBand.ui.view

import android.content.Context
import android.graphics.Bitmap
import android.text.InputType
import android.text.TextUtils
import android.util.AttributeSet
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.*
import butterknife.BindView
import butterknife.ButterKnife
import com.zztl.infiniteBand.R
import com.zztl.infiniteBand.util.SizeUtils

/**
 * 通用Item
 */

class CommonItemView2 : LinearLayout {

    @BindView(R.id.common_itemview_container)
    lateinit var containerLayout: RelativeLayout
    @BindView(R.id.common_itemview_left_layout)
    lateinit var leftLayoutLayout: RelativeLayout
    @BindView(R.id.common_itemview_left_img)
    lateinit var leftImg: ImageView
         set
    @BindView(R.id.common_itemview_left_text)
    lateinit var leftTv: TextView
         set
    @BindView(R.id.common_itemview_left2_img)
    lateinit var leftImg2: ImageView

    @BindView(R.id.common_itemview_middle_layout)
    lateinit  var middleLayout: RelativeLayout
    @BindView(R.id.common_itemview_middle_edit)
    lateinit var middleEdit: EditText
         set
    @BindView(R.id.common_itemview_middle_tv)
    lateinit var middleTv: TextView
         set

    @BindView(R.id.common_itemview_right_layout)
    lateinit  var rightLayout: RelativeLayout
    @BindView(R.id.common_itemview_right_img)
    lateinit var rightImg: ImageView
         set
    @BindView(R.id.common_itemview_right_img2)
    lateinit var rightImg2: ImageView
    @BindView(R.id.common_itemview_right_tv)
    lateinit var rightTv: TextView
         set
    @BindView(R.id.common_itemview_right_btn)
    lateinit var rightButton: Button
         set

    @BindView(R.id.common_itemview_bottom)
    lateinit var bottomView: View

    var conText: Context
    /**
     * ID
     */
    protected var viewID: Int = 0
    /**
     * 标题样式
     */
    protected var viewType: Int = 0
    /**
     * 按纽样式
     */
    protected var buttonType: Int = 0
    /**
     * 输入类型
     */
    protected var inType: Int = 0

    private var itemClickListener: OnItemClickListener? = null
    private var itemRightClickListener: OnItemRightClickListener? = null
    private var itemLeftClickListener: OnItemLeftClickListener? = null

    val middleEditStr: String
        get() = middleEdit.text.toString().trim { it <= ' ' }

     val middleEditStr2: String
        get() = middleEdit.text.toString()

    var rightTextStr: String?
        get() = rightTv.text.toString().trim { it <= ' ' }
        set(str) {
            rightTv.text = if (!TextUtils.isEmpty(str)) str else ""
        }

    val widthRightImage: Int
        get() = rightImg.width

    val heightRightImage: Int
        get() = rightImg.height

    constructor(context: Context) : super(context) {
        this.conText = context
        init()
    }

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        this.conText = context
        init()
        setAttrs(attrs)
    }



    private fun init() {
        View.inflate(context, R.layout.common_itemview_layout2, this)
        ButterKnife.bind(this)
        setBackgroundColor(context.resources.getColor(R.color.color_ffffff))
    }

    private fun setAttrs(attrs: AttributeSet) {
        viewID = id
        val attributes = context.obtainStyledAttributes(attrs, R.styleable.CommonItemView)
        viewType = attributes.getInt(R.styleable.CommonItemView_commonitem_type, LEFT_ICON_MIDDLE_EDIT)
        buttonType = attributes.getInt(R.styleable.CommonItemView_commonitem_right_button, BUTTON_STYLE_ONE)
        inType = attributes.getInt(R.styleable.CommonItemView_commonitem_middle_edit_type, INPUT_TYPE_TEXT)

        setLeftImg(attributes.getResourceId(R.styleable.CommonItemView_commonitem_left_img, R.mipmap.xiayibu_icon))
        setLeftImg2(attributes.getResourceId(R.styleable.CommonItemView_commonitem_left_img2, R.mipmap.xiayibu_icon))
        setLeftTextColor(attributes.getColor(R.styleable.CommonItemView_commonitem_left_text_color, context.resources.getColor(R.color.color_1B1100)))
        setLeftTextStr(attributes.getString(R.styleable.CommonItemView_commonitem_left_text_string))
        setLeftTextStrSize(attributes.getDimension(R.styleable.CommonItemView_commonitem_left_text_size, 15f))

        setMiddleTextColor(attributes.getColor(R.styleable.CommonItemView_commonitem_middle_text_color, context.resources.getColor(R.color.color_1B1100)))
        setMiddleTextStr(attributes.getString(R.styleable.CommonItemView_commonitem_middle_text_string))
        setMiddleEditHintColor(attributes.getColor(R.styleable.CommonItemView_commonitem_middle_hint_color, context.resources.getColor(R.color.color_ACA593)))
        setMiddleEditHintStr(attributes.getString(R.styleable.CommonItemView_commonitem_middle_hint))
        setMiddleEditTextColor(attributes.getColor(R.styleable.CommonItemView_commonitem_middle_hint_text_color, context.resources.getColor(R.color.color_1B1100)))
        setMiddleEditTextStr(attributes.getString(R.styleable.CommonItemView_commonitem_middle_edit_string))
        setMiddleEditTextSize(attributes.getDimension(R.styleable.CommonItemView_commonitem_middle_edit_size, 15f))
        setMiddleEditInputAble(attributes.getBoolean(R.styleable.CommonItemView_commonitem_middle_input_able, true))
        setMiddleEditLenght(attributes.getInteger(R.styleable.CommonItemView_commonitem_middle_input_maxems, -1))

        setRightTextColor(attributes.getColor(R.styleable.CommonItemView_commonitem_right_text_color, context.resources.getColor(R.color.color_1B1100)))
        rightTextStr = attributes.getString(R.styleable.CommonItemView_commonitem_right_text_string)
        setRightTextSize(attributes.getDimension(R.styleable.CommonItemView_commonitem_left_text_size, 15f))
        setRightImg(attributes.getResourceId(R.styleable.CommonItemView_commonitem_right_img, R.mipmap.xiayibu_icon))
        setRightImgSize(attributes.getLayoutDimension(R.styleable.CommonItemView_commonitem_right_img_width, 0), //
                attributes.getLayoutDimension(R.styleable.CommonItemView_commonitem_right_img_height, 0))
        setButtonStr(attributes.getString(R.styleable.CommonItemView_commonitem_right_button_string))

        setRight2Img(attributes.getResourceId(R.styleable.CommonItemView_commonitem_right2_img, R.mipmap.xiayibu_icon))
        setBottomLine(attributes.getBoolean(R.styleable.CommonItemView_commonitem_bottom_line, true))
        setItemClickable(attributes.getBoolean(R.styleable.CommonItemView_commonitem_clickable, false))

        initView(viewType, 0)
        setInputType(inType)
        //        setItemLeftClick();
        setRightImageClick()
        setRightTextClick()
        setRightButtonClick()

        attributes.recycle()
    }


    /**
     * 除了xml初始化外，动态设置样式
     */

    fun initView(type: Int, bType: Int) {
        viewType = type
        buttonType = bType//没有利用，待补充
        when (type) {
            MIDDLE_EDIT -> {
                leftLayoutLayout.visibility = View.GONE
                middleEdit.visibility = View.VISIBLE
                middleTv.visibility = View.GONE
                rightLayout.visibility = View.GONE
                rightImg2.visibility = View.GONE
            }
            MIDDLE_EDIT_RIGHT_TEXT -> {
                leftLayoutLayout.visibility = View.GONE
                middleEdit.visibility = View.VISIBLE
                middleTv.visibility = View.GONE
                rightImg.visibility = View.GONE
                rightTv.visibility = View.VISIBLE
                rightButton.visibility = View.GONE
                rightImg2.visibility = View.GONE
            }

            MIDDLE_EDIT_RIGHT_ICON -> {
                leftLayoutLayout.visibility = View.GONE
                middleEdit.visibility = View.VISIBLE
                middleTv.visibility = View.GONE
                rightImg.visibility = View.VISIBLE
                rightTv.visibility = View.VISIBLE
                rightButton.visibility = View.GONE
                rightImg2.visibility = View.GONE
            }

            LEFT_ICON_MIDDLE_EDIT -> {
                leftImg.visibility = View.VISIBLE
                leftTv.visibility = View.GONE
                leftImg2.visibility = View.GONE
                middleEdit.visibility = View.VISIBLE
                middleTv.visibility = View.GONE
                rightLayout.visibility = View.GONE
                rightImg2.visibility = View.GONE
            }
            LEFT_TEXT_MIDDLE_EDIT -> {
                leftTv.visibility = View.VISIBLE
                leftImg.visibility = View.GONE
                leftImg2.visibility = View.GONE
                middleEdit.visibility = View.VISIBLE
                middleTv.visibility = View.GONE
                rightLayout.visibility = View.GONE
                rightImg2.visibility = View.GONE
            }
            LEFT_ICON_MIDDLE_TEXT_RIGHT_ICON -> {
                leftImg.visibility = View.VISIBLE
                leftImg2.visibility = View.GONE
                leftTv.visibility = View.GONE
                middleEdit.visibility = View.GONE
                middleTv.visibility = View.VISIBLE
                rightImg.visibility = View.VISIBLE
                rightTv.visibility = View.GONE
                rightButton.visibility = View.GONE
                rightImg2.visibility = View.GONE
            }
            LEFT_TEXT_MIDDLE_EDIT_RIGHT_ICON -> {
                leftTv.visibility = View.VISIBLE
                leftImg.visibility = View.GONE
                leftImg2.visibility = View.GONE
                middleEdit.visibility = View.VISIBLE
                middleTv.visibility = View.GONE
                rightImg.visibility = View.VISIBLE
                rightTv.visibility = View.GONE
                rightButton.visibility = View.GONE
                rightImg2.visibility = View.GONE
            }
            LEFT_TEXT_MIDDLE_EDIT_RIGHT_BUTTON -> {
                leftTv.visibility = View.VISIBLE
                leftImg.visibility = View.GONE
                leftImg2.visibility = View.GONE
                middleEdit.visibility = View.VISIBLE
                middleTv.visibility = View.GONE
                rightImg.visibility = View.GONE
                rightTv.visibility = View.GONE
                rightButton.visibility = View.VISIBLE
                rightImg2.visibility = View.GONE
            }
            LEFT_TEXT_ICON_MIDDLE_EDIT -> {
                leftTv.visibility = View.VISIBLE
                leftImg2.visibility = View.VISIBLE
                leftImg.visibility = View.GONE
                middleEdit.visibility = View.VISIBLE
                middleTv.visibility = View.GONE
                rightLayout.visibility = View.GONE
                rightImg2.visibility = View.GONE
            }
            LEFT_TEXT_MIDDLE_TEXT -> {
                leftTv.visibility = View.VISIBLE
                leftImg.visibility = View.GONE
                leftImg2.visibility = View.GONE
                middleEdit.visibility = View.GONE
                middleTv.visibility = View.VISIBLE
                rightLayout.visibility = View.GONE
                rightImg2.visibility = View.GONE
            }
            LEFT_TEXT_RIGHT_TEXT -> {
                leftTv.visibility = View.VISIBLE
                leftImg.visibility = View.GONE
                leftImg2.visibility = View.GONE
                middleLayout.visibility = View.GONE
                rightImg.visibility = View.GONE
                rightTv.visibility = View.VISIBLE
                rightButton.visibility = View.GONE
                rightImg2.visibility = View.GONE
            }
            LEFT_TEXT_MIDDLE_EDIT_RIGHT_TEXT -> {
                leftTv.visibility = View.VISIBLE
                leftImg.visibility = View.GONE
                leftImg2.visibility = View.GONE
                middleEdit.visibility = View.VISIBLE
                middleTv.visibility = View.GONE
                rightImg.visibility = View.GONE
                rightTv.visibility = View.VISIBLE
                rightButton.visibility = View.GONE
                rightImg2.visibility = View.GONE
            }
            LEFT_TEXT_RIGHT_ICON -> {
                leftTv.visibility = View.VISIBLE
                leftImg.visibility = View.GONE
                leftImg2.visibility = View.GONE
                middleLayout.visibility = View.GONE
                rightImg.visibility = View.VISIBLE
                rightTv.visibility = View.GONE
                rightButton.visibility = View.GONE
                rightImg2.visibility = View.GONE
            }
            LEFT_TEXT_RIGHT_TEXT_ICON -> {
                leftTv.visibility = View.VISIBLE
                leftImg.visibility = View.GONE
                leftImg2.visibility = View.GONE
                middleLayout.visibility = View.GONE
                rightImg.visibility = View.GONE
                rightTv.visibility = View.VISIBLE
                rightButton.visibility = View.GONE
                rightImg2.visibility = View.VISIBLE
            }
            LEFT_TEXT_MIDDLE_TEXT_RIGHT_ICON -> {
                leftTv.visibility = View.VISIBLE
                leftImg.visibility = View.GONE
                leftImg2.visibility = View.GONE
                middleEdit.visibility = View.GONE
                middleTv.visibility = View.VISIBLE
                rightImg.visibility = View.VISIBLE
                rightTv.visibility = View.VISIBLE
                rightButton.visibility = View.GONE
                rightImg2.visibility = View.GONE
            }
            LEFT_TEXT_RIGHT_UPDATE -> {
                leftTv.visibility = View.VISIBLE
                leftImg.visibility = View.GONE
                leftImg2.visibility = View.GONE
                middleLayout.visibility = View.GONE
                rightImg.visibility = View.GONE
                rightTv.visibility = View.GONE
                rightButton.visibility = View.GONE
                rightImg2.visibility = View.GONE
            }
            LEFT_ICON_TEXT_RIGHT_ICON -> {
                leftTv.visibility = View.VISIBLE
                leftImg.visibility = View.VISIBLE
                leftImg2.visibility = View.GONE
                middleEdit.visibility = View.GONE
                middleTv.visibility = View.GONE
                rightImg.visibility = View.VISIBLE
                rightTv.visibility = View.GONE
                rightButton.visibility = View.GONE
                rightImg2.visibility = View.GONE
            }
            LEFT_ICON_TEXT_RIGHT_TEXT_ICON -> {
                leftTv.visibility = View.VISIBLE
                leftImg.visibility = View.VISIBLE
                leftImg2.visibility = View.GONE
                middleEdit.visibility = View.GONE
                middleTv.visibility = View.GONE
                rightImg.visibility = View.GONE
                rightTv.visibility = View.VISIBLE
                rightButton.visibility = View.GONE
                rightImg2.visibility = View.VISIBLE
            }
            LEFT_ICON_TEXT_RIGHT_TEXT -> {
                leftTv.visibility = View.VISIBLE
                leftImg.visibility = View.VISIBLE
                leftImg2.visibility = View.GONE
                middleEdit.visibility = View.GONE
                middleTv.visibility = View.GONE
                rightImg.visibility = View.GONE
                rightTv.visibility = View.VISIBLE
                rightButton.visibility = View.GONE
                rightImg2.visibility = View.GONE
            }
            else -> {
            }
        }//                rightUpdateView.setVisibility(GONE);
        //                rightUpdateView.setVisibility(GONE);
        //                rightUpdateView.setVisibility(GONE);
        //                rightUpdateView.setVisibility(GONE);
        //                rightUpdateView.setVisibility(GONE);
        //                rightUpdateView.setVisibility(GONE);
        //                rightUpdateView.setVisibility(GONE);
        //                rightUpdateView.setVisibility(GONE);
        //                rightUpdateView.setVisibility(GONE);
        //                rightUpdateView.setVisibility(GONE);
        //                rightUpdateView.setVisibility(GONE);
        //                rightUpdateView.setVisibility(GONE);
    }

    fun setInputType(type: Int) {
        inType = type
        when (inType) {
            INPUT_TYPE_PHONE -> middleEdit.inputType = InputType.TYPE_CLASS_PHONE

            INPUT_TYPE_NUMBER -> middleEdit.inputType = InputType.TYPE_CLASS_NUMBER

            INPUT_TYPE_NUMBER_DECIMAL -> middleEdit.inputType = InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_DECIMAL

            INPUT_TYPE_TEXT -> middleEdit.inputType = InputType.TYPE_CLASS_TEXT

            INPUT_TYPE_TEXT_PWD -> middleEdit.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD

        }
    }

    /**
     * 设置
     */
    fun setWrapContent() {
        val params = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        containerLayout.minimumHeight = SizeUtils.dp2px(50f)
        containerLayout.layoutParams = params
    }

    /**
     * 设置固定大小
     */
    fun setFixedWidth(justLeft: Boolean, widh: Int) {
        val params = RelativeLayout.LayoutParams(SizeUtils.dp2px((if (widh == 0) 145 else widh).toFloat()), ViewGroup.LayoutParams.WRAP_CONTENT)
        params.addRule(RelativeLayout.CENTER_VERTICAL)
        leftTv.gravity = Gravity.START
        leftTv.layoutParams = params

        if (!justLeft) {
            val params2 = RelativeLayout.LayoutParams(SizeUtils.dp2px(145f), ViewGroup.LayoutParams.WRAP_CONTENT)
            params2.addRule(RelativeLayout.CENTER_VERTICAL)
            rightTv.gravity = Gravity.END
            rightTv.layoutParams = params2
        }
    }


    fun getInputType(): Int {
        return inType
    }


    fun setLeftImg(res: Int) {
        leftImg.setImageResource(res)
    }

    fun setLeftImg2(res: Int) {
        leftImg2.setImageResource(res)
    }

    fun setLeftTextColor(color: Int) {
        leftTv.setTextColor(color)
    }

    fun setLeftTextStr(str: String?) {
        str?.let {
            leftTv.text = it
        }

    }


    fun setLeftTextStrSize(demesion: Float) {
        leftTv.textSize = demesion
    }

    fun setRightTextSize(demesion: Float) {
        rightTv.textSize = demesion
    }

    fun setMiddleTextColor(color: Int) {
        middleTv.setTextColor(color)
    }

    fun setMiddleTextStr(str: String?) {
        str?.let {
            middleTv.text=it
        }
    }

    fun setMiddleEditHintColor(color: Int) {
        middleEdit.setTextColor(color)
    }

    fun setMiddleEditHintStr(str: String?) {
        str?.let {
            middleEdit.hint=it
        }
    }

    fun setMiddleEditTextColor(color: Int) {
        middleEdit.setTextColor(color)
    }

    fun setMiddleEditTextStr(str: String?) {
        str?.let {
            middleEdit.setText(it)
            middleEdit.setSelection(if (str.isNotEmpty()) it.length else 0)
        }

    }

    fun setMiddleEditTextSize(demesion: Float) {
        middleEdit.textSize = demesion

    }


    private fun setMiddleEditInputAble(aBoolean: Boolean) {
        if (aBoolean) {
            return
        }
        //        middleEdit.setEnabled(aBoolean);
        //        middleEdit.setClickable(aBoolean);
        middleEdit.isFocusable = aBoolean
        middleEdit.isFocusableInTouchMode = aBoolean
    }

    private fun setMiddleEditLenght(maxEms: Int) {
        //        if (maxEms != -1) {
        //            middleEdit.setMaxEms(maxEms);
        //        }
    }

    fun setRightTextColor(color: Int) {
        rightTv.setTextColor(color)
    }

    fun setRightImg(res: Int) {
        rightImg.setImageResource(res)
    }

    fun setRightImg2Visible(visible: Boolean) {
        rightImg2.visibility = if (visible) View.VISIBLE else View.GONE
    }


    fun setRightImgSize(width: Int, height: Int) {
        if (width != 0 || height != 0) {
            val params = RelativeLayout.LayoutParams(width, height)
            params.addRule(RelativeLayout.CENTER_VERTICAL)
            rightImg.layoutParams = params
        }
    }

    fun setRightImgBitmap(bm: Bitmap) {
        rightImg.setImageBitmap(bm)
    }

    fun setRight2Img(res: Int) {
        rightImg2.setImageResource(res)
    }

    fun setBottomLine(isShow: Boolean) {
        bottomView.visibility = if (isShow) View.VISIBLE else View.GONE
    }

    fun setItemClickable(isClickable: Boolean) {
        if (isClickable) {
            leftImg.isClickable = false
            leftTv.isClickable = false
            leftImg.isClickable = false
            leftImg2.isClickable = false
            middleEdit.isClickable = false
            middleTv.isClickable = false
            rightImg.isClickable = false
            rightImg2.isClickable = false
            rightTv.isClickable = false
            rightButton.isClickable = false

            containerLayout.isFocusable = true
            containerLayout.isClickable = true
            containerLayout.isFocusableInTouchMode = true
            containerLayout.setOnClickListener {

                    itemClickListener?.onClickItem(viewID)

            }
        }
    }

    private fun setItemLeftClick() {
        leftLayoutLayout.isClickable = true
        leftLayoutLayout.setOnClickListener {

                itemLeftClickListener?.onClickItemLeft(viewID)

        }
    }

    private fun setRightImageClick() {
        rightImg.setOnClickListener {

                itemRightClickListener?.onClickItemRight(viewID)

        }
    }

    private fun setRightTextClick() {
        rightTv.setOnClickListener {
                itemRightClickListener?.onClickItemRight(viewID)
        }
    }

    private fun setRightButtonClick() {
        if (viewType == LEFT_TEXT_MIDDLE_EDIT_RIGHT_BUTTON) {
            rightLayout.setOnClickListener {
                    itemRightClickListener?.onClickItemRight(viewID)
            }
        }
    }

    fun setButtonStr(str: String?) {
        str?.let {
            rightButton.text=it
        }
    }


    fun setItemClickListener(listener: OnItemClickListener) {
        this.itemClickListener = listener
    }

    fun setItemRightClickListener(listener: OnItemRightClickListener) {
        this.itemRightClickListener = listener
    }

    fun setItemLeftClickListener(listener: OnItemLeftClickListener) {
        this.itemLeftClickListener = listener
    }

    interface OnItemLeftClickListener {
        fun onClickItemLeft(viewID: Int)
    }

    interface OnItemRightClickListener {
        fun onClickItemRight(viewID: Int)
    }

    interface OnItemClickListener {
        fun onClickItem(viewID: Int)
    }

    companion object {

        val LEFT_ICON_MIDDLE_EDIT = 0
        val LEFT_TEXT_MIDDLE_EDIT = 1
        val LEFT_ICON_MIDDLE_TEXT_RIGHT_ICON = 2
        val LEFT_TEXT_MIDDLE_EDIT_RIGHT_ICON = 3
        val LEFT_TEXT_MIDDLE_EDIT_RIGHT_BUTTON = 4
        val LEFT_TEXT_ICON_MIDDLE_EDIT = 5
        val LEFT_TEXT_MIDDLE_TEXT = 6
        val LEFT_TEXT_RIGHT_TEXT = 7
        val LEFT_TEXT_MIDDLE_EDIT_RIGHT_TEXT = 8
        val LEFT_TEXT_RIGHT_ICON = 9
        val LEFT_TEXT_RIGHT_TEXT_ICON = 10
        val LEFT_TEXT_MIDDLE_TEXT_RIGHT_ICON = 11
        val LEFT_TEXT_RIGHT_UPDATE = 12
        val MIDDLE_EDIT = 13
        val MIDDLE_EDIT_RIGHT_TEXT = 14
        val LEFT_ICON_TEXT_RIGHT_ICON = 15
        val LEFT_ICON_TEXT_RIGHT_TEXT_ICON = 16
        val LEFT_ICON_TEXT_RIGHT_TEXT = 17
        val MIDDLE_EDIT_RIGHT_ICON = 18


        //Edit 输入的文字类型
        val INPUT_TYPE_TEXT = 0
        val INPUT_TYPE_NUMBER = 1
        val INPUT_TYPE_PHONE = 2
        val INPUT_TYPE_TEXT_PWD = 3
        val INPUT_TYPE_NUMBER_DECIMAL = 4

        //右边按钮类型
        val BUTTON_STYLE_ONE = 41
        val BUTTON_STYLE_TWO = 42
    }
}
